# coding: utf-8
from config.settings import database,get_auto_id
from datetime import datetime
from app.helper import util

collection=database["admin"]


def list(page=1,limit=5):
    offset = (int(page)-1)*limit
    return collection.find().limit(limit).skip(offset)

def save(i):
    if i.admin_id==0:
       i.admin_id=get_auto_id('admin')
       return collection.insert(i)
    else:
       return collection.update({"admin_id":i.admin_id},{"$set":i})
     

def find_one(id):
    return collection.find_one({"admin_id":id})

def login(username, password):
    user = collection.find_one({'username':username, 'password':password})
    return user

def check_username(admin_id,username):
    user = collection.find_one({'admin_id':{"$ne":admin_id},'username':username})
    return user


